//
//  QuizTalkAlert.m
//  TableAlertDemo
//
//  Created by Tianlong on 2018/1/26.
//  Copyright © 2018年 Tianlong. All rights reserved.
//

#import "QuizTalkAlert.h"
#import "Masonry.h"

#define DefaultUIH 237  // UI设计高度
#define DefaultUIW 295  // UI设计宽度
#define DefaultLabelLeftRightM 20
#define CoverAlpha 0.4
#define AnimateDuration .25
#define TextLength 200
#define TextFontSize 13

@interface QuizTalkAlert ()
@property (weak, nonatomic) UIView *inView;
@property (assign, nonatomic) BOOL animate;
@property (weak, nonatomic) UIButton *coverBtn;

// 顶部老师姓名
@property (weak, nonatomic) IBOutlet UILabel *titleL;
@property (weak, nonatomic) IBOutlet UIImageView *iconV;
@property (weak, nonatomic) IBOutlet UILabel *descL;

@end

@implementation QuizTalkAlert

- (void)awakeFromNib{
    [super awakeFromNib];
    self.layer.cornerRadius = 5;
    self.layer.masksToBounds = YES;
}

+ (instancetype)alertInView:(UIView *)inView info:(id)info{
    
    QuizTalkAlert *alert = [QuizTalkAlert loadFromNib];
    
    if ([inView isKindOfClass:[UIWindow class]]) {
        //创建遮盖
        UIButton *coverBtn = [[UIButton alloc] init];
        coverBtn.alpha = 0;
        coverBtn.backgroundColor = [UIColor blackColor];
        [inView addSubview:coverBtn];
        [coverBtn addTarget:alert action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];
        [coverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.right.equalTo(inView);
        }];
        alert.coverBtn = coverBtn;
    }
    
    [inView addSubview:alert];
    
    alert.titleL.text = @"王乐乐老师有话说";
    alert.iconV.backgroundColor = [UIColor greenColor];
    NSString *desc = @"近两年，剧集进入大制作时代，“酒香也怕巷子深”，动辄上亿的投资催生出了各种千万级的宣传战。在这场战役中，打头阵的毫无疑问是海报。作为剧集的名片和门脸，在剧正式播出之前，海报就是";
    if (desc.length > TextLength) {
        alert.descL.text = [desc substringToIndex:TextLength];
    }else{
        alert.descL.text = desc;
    }
    
    CGSize textS = [alert.descL sizeThatFits:CGSizeMake(DefaultUIW-DefaultLabelLeftRightM*2, MAXFLOAT)];
    CGFloat labelH = textS.height+20;
    CGFloat realH = 40+17+30+40+labelH+40;//参考UI设计
    [alert mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(inView.mas_centerX);
        make.centerY.equalTo(inView.mas_centerY);
        make.width.offset(DefaultUIW);
        make.height.offset(realH);
    }];
    alert.alpha = 0;
    return alert;
}

+ (instancetype)alertInWindowWithInfo:(id)info{
    UIWindow *win = [UIApplication sharedApplication].keyWindow;
    QuizTalkAlert *alert = [QuizTalkAlert alertInView:win info:info];
    return alert;
}

- (IBAction)close:(id)sender {
    [self dismissWithAnimate:NO];
}

- (void)showWithAnimate:(BOOL)animate{
    __weak typeof(self)ws = self;
    if (animate) {
        self.alpha = 1;
        self.transform = CGAffineTransformMakeScale(1.1, 1.1);
        [UIView animateWithDuration:AnimateDuration delay:0 usingSpringWithDamping:0.4 initialSpringVelocity:20 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            ws.transform = CGAffineTransformIdentity;
            if(ws.coverBtn){
                ws.coverBtn.alpha = CoverAlpha;
            }
        }completion:nil];
    }else{
        self.alpha = 1;
        if(self.coverBtn){
            self.coverBtn.alpha = CoverAlpha;
        }
    }
}

- (void)dismissWithAnimate:(BOOL)animate{
    __weak typeof(self)ws = self;
    if (animate) {
        [UIView animateWithDuration:AnimateDuration animations:^{
            ws.alpha = 0;
            ws.coverBtn.alpha = 0;
        }completion:^(BOOL finished) {
            [ws removeFromSuperview];
            [ws.coverBtn removeFromSuperview];
        }];
    }else{
        [self removeFromSuperview];
        [self.coverBtn removeFromSuperview];
    }
}

@end
